package com.sea.finance.service.impl;


import com.sea.finance.entity.PaymentIncreaseLoss;
import com.sea.finance.mapper.PaymentIncreaseLossMapper;
import com.sea.finance.service.ExchangeRateRecordService;
import com.sea.finance.service.PaymentIncreaseLossService;
import com.sea.supply.entity.ProducePayment;
import com.sea.supply.service.ProducePaymentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class PaymentIncreaseLossServiceImpl implements PaymentIncreaseLossService {

    @Autowired
    private PaymentIncreaseLossMapper paymentIncreaseLossMapper;
    @Autowired
    private ProducePaymentService producePaymentService;
    @Autowired
    private ExchangeRateRecordService rateRecordService;

    @Override
    public List<PaymentIncreaseLoss> findByCnd(PaymentIncreaseLoss payment) {
        return paymentIncreaseLossMapper.findByCnd(payment);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(PaymentIncreaseLoss payment) {
        if (payment.getProduceRemark().equals("预付款")) {
            Integer count = findFirstByProducePaymentCount(payment.getProduceId());
            if (null==count || count == 0) {
               return paymentIncreaseLossMapper.insert(payment);
            }else if(count>1){
               return deleteFirstByProducePayment(payment.getProduceId());
            }else {
                return 1;
            }
        }else{
            return paymentIncreaseLossMapper.insert(payment);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDetail(Integer paymentId,String payTime,Double producePay) {
        ProducePayment producePayment = producePaymentService.findById(paymentId);
        PaymentIncreaseLoss loss=new PaymentIncreaseLoss();
        //预付款

        if(producePayment.getProduceRemark().equals("预付款")) {
            //更新付款信息
            loss.setCreatePeriod(producePayment.getCreateTime().substring(0, 7));
            loss.setPayPeriod(payTime.substring(0, 7));
            switch (producePayment.getProduceMoneyType()) {
                case "美金":
                    loss.setCreateRate(rateRecordService.findByPeriod(1,loss.getCreatePeriod()).getRatePercent());
                    loss.setPayRate(rateRecordService.findByPeriod(1,loss.getPayPeriod()).getRatePercent());
                    if(producePay.equals(0.00)){
                        loss.setProducePay(0.00);
                    }else{
                        loss.setProducePay(producePay+producePayment.getProduceUnusualMoney());
                    }
                    break;
                case "人民币" :
                    loss.setCreateRate(1.0);
                    loss.setPayRate(1.0);
                    if(producePay.equals(0.00)){
                        loss.setProducePay(0.00);
                    }else{
                        loss.setProducePay(producePay+producePayment.getProduceUnusualMoney());
                    }
                    break;
                case "欧元":
                    loss.setCreateRate(rateRecordService.findByPeriod(4,loss.getCreatePeriod()).getRatePercent());
                    loss.setPayRate(rateRecordService.findByPeriod(4,loss.getPayPeriod()).getRatePercent());
                    if(producePay.equals(0.00)){
                        loss.setProducePay(0.00);
                    }else{
                        loss.setProducePay(producePay+producePayment.getProduceUnusualMoney());
                    }
                    break;
            }
            loss.setProduceBalance((loss.getPayRate()*loss.getProducePay())-loss.getCreateRate()*producePayment.getProducePayment());
        }else{
            String createPeriod = findCreatePeriodByCreatePlanNo(producePayment.getCreatePlanNo());
            //更新付款信息
            if(StringUtils.isBlank(createPeriod)){
                loss.setCreatePeriod(payTime.substring(0, 7)  );
                createPeriod=payTime.substring(0, 7);
            }else {
                loss.setCreatePeriod(createPeriod);
            }
            loss.setPayPeriod(payTime.substring(0, 7));
            if(StringUtils.isNotBlank(createPeriod)){
                switch (producePayment.getProduceMoneyType()) {
                    case "美金":
                        loss.setCreateRate(rateRecordService.findByPeriod(1,loss.getCreatePeriod()).getRatePercent());
                        loss.setPayRate(rateRecordService.findByPeriod(1,loss.getPayPeriod()).getRatePercent());
                        loss.setProducePay(producePay+producePayment.getProduceUnusualMoney());
                        break;
                    case "人民币" :
                        loss.setCreateRate(1.0);
                        loss.setPayRate(1.0);
                        loss.setProducePay(producePay+producePayment.getProduceUnusualMoney());
                        break;
                    case "欧元":
                        loss.setCreateRate(rateRecordService.findByPeriod(4,loss.getCreatePeriod()).getRatePercent());
                        loss.setPayRate(rateRecordService.findByPeriod(4,loss.getPayPeriod()).getRatePercent());
                        loss.setProducePay(producePay+producePayment.getProduceUnusualMoney());
                        break;
                }
                loss.setProduceBalance(loss.getCreateRate()*producePayment.getProducePayment()-(loss.getPayRate()*loss.getProducePay()));
            }else{
                throw new RuntimeException("暂未获取到出货期间,请联系管理员!");
            }

//            PaymentIncreaseLoss first = findFirstByProduceId(producePayment.getProduceId());
//            if(null==first){
//                throw new RuntimeException("暂未获取到预付款数据,请先付款该PO预付款数据!");
//            }

            loss.setProduceBalance((loss.getPayRate()*producePayment.getProduceCheckMoney()+loss.getPayRate()*loss.getProducePay())-loss.getCreateRate()*producePayment.getProducePayment());
        }

        loss.setPaymentId(producePayment.getId());
        loss.setProduceRemark(producePayment.getProduceRemark());
        loss.setProduceId(producePayment.getProduceId());
        //更新汇率损益
        save(loss);
    }

    @Override
    public String findCreatePeriodByCreatePlanNo(String createPlanNo) {
        return paymentIncreaseLossMapper.findCreatePeriodByCreatePlanNo(createPlanNo);
    }

    @Override
    public PaymentIncreaseLoss findFirstByProduceId(Integer produceId) {
        return paymentIncreaseLossMapper.findFirstByProduceId(produceId);
    }

    @Override
    public Integer delete(Integer id) {
        return paymentIncreaseLossMapper.delete(id);
    }

    @Override
    public Integer findFirstByProducePaymentCount(Integer produceId) {
        return paymentIncreaseLossMapper.findFirstByProducePaymentCount(produceId);
    }

    @Override
    public Integer deleteFirstByProducePayment(Integer produceId) {
        return paymentIncreaseLossMapper.deleteFirstByProducePayment(produceId);
    }

}
